package models

import (
	"gitee.com/sansaniot/ssiot-core/httpmvc/model"

	cDto "ssalarm/common/dto"
	"ssalarm/internal/alarm/models/query"
)

type SysDevProduct struct {
	Id           string          `json:"id" gorm:"primaryKey;type:varchar(255);comment:id"`
	Name         string          `json:"name" gorm:"type:varchar(255);comment:产品名称,具有唯一性(如定义为产品型号)"`
	DeviceType   string          `json:"deviceType" gorm:"type:varchar(255);comment:网关:gateway 设备:device 软网关:soft_gateway"`
	Status       bool            `json:"status" gorm:"type:bool;comment:是否已发布"`
	ProductModel string          `json:"model" gorm:"column:model;type:varchar(255);comment:产品编码"`
	DeptId       string          `json:"deptId" gorm:"type:varchar(255);comment:分组id"`
	Photo        string          `json:"photo" gorm:"type:varchar(255);comment:产品图片"`
	Description  string          `json:"description" gorm:"type:text;comment:描述"`
	CreateTime   *cDto.LocalTime `json:"createTime" gorm:"type:timestamptz(6);comment:创建时间"`
	UpdateTime   *cDto.LocalTime `json:"updateTime" gorm:"type:timestamptz(6);comment:更新时间"`
	model.Model
}

func (SysDevProduct) TableName() string {
	return "sys_dev_product"
}

func (e *SysDevProduct) Generate() interface{} {
	o := *e
	return &o
}

func (e *SysDevProduct) GetId() interface{} {
	return e.Id
}

func (e *SysDevProduct) FindProductList(req *query.SysProductQuery) (err error, result interface{}) {
	list := make([]SysDevProduct, 0)
	var count int64
	if req.PageIndex > 0 && req.PageSize > 0 {
		err = e.FindPage(&list, &count,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
			cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		)
		result = &cDto.Page{
			Records:   list,
			Total:     count,
			PageSize:  req.GetPageSize(),
			PageIndex: req.GetPageIndex(),
			Pages:     (count + int64(req.GetPageSize()) - 1) / int64(req.GetPageSize()),
			Other:     nil,
		}
	} else {
		err = e.FindList(&list,
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.OrderDest("create_time", true, req.OrderBy),
		)
		result = list
	}
	return
}

func (e *SysDevProduct) FindProduct(req *query.SysProductQuery) (err error, one SysDevProduct) {
	err = e.FindOne(&one, cDto.MakeCondition(req.GetNeedSearch()))
	return
}

func (e *SysDevProduct) GetNameMapper() (mapper map[string]string) {
	mapper = make(map[string]string)
	list := make([]SysDevProduct, 0)
	if err := e.FindList(&list); err != nil {
		return
	}
	for _, v := range list {
		mapper[v.ProductModel] = v.Name
	}
	return
}
